<template>
<div class="main_box"  style="padding:10px">
  <div class="add_wrap">
    <div class="tit">
      <h2>新增设备</h2>
      <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
    </div>
    <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules">
      <el-row>

        <el-form-item label="标本箱名称" prop="boxName">
          <el-input v-model="dataForm.boxName" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
        <el-form-item label="标本箱编号" prop="boxCode">
          <el-input v-model="dataForm.boxCode" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
        <el-form-item label="收样员" prop="syUserId">
          <el-select v-model="dataForm.syUserId" placeholder="请选择收样员">
            <el-option
              v-for="item in syUserList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="温控设备" prop="etId">
          <el-select v-model="dataForm.etId" placeholder="请选择温控设备">
            <el-option
              v-for="item in temperatureList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="医院类型" prop="classifyId">
          <ClassifyRegion :classifyValues="dataForm.classifyId" @classifyRegionChange="handleClassifyChange"></ClassifyRegion>
        </el-form-item>-->
      </el-row>

      <el-row>
        <el-form-item>
          <el-button plain type="primary" @click="sureClick">保 存</el-button>
          <el-button plain @click="returnList">取 消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
 </div>
</template>

<script>
import * as message from "@/constants/message";
import Region from "@/components/Region";
import EquipApi from '@/api/equip/equip';
import AuthApi from "@/api/system/auth";

export default{
  data(){
      return {
        dataForm:{
          esId: "",
          boxName: "",
          boxCode: "",
          syUserId: "",
          etId: "",
          boxType: ""
        },
        loadingIns:false,
        syUserId: null,
        syUserList: [],
        temperatureList: [],
        roleRules: {
          boxName: [
            { required: true, message: "请输入标本箱名称", trigger: "blur" },
            { max: 30, message: '长度不超过30个字', trigger: 'blur' }
          ],
          boxCode: [
            { required: true, message: "请输入标本箱编号", trigger: "blur" },
            { max: 20, message: '长度不超过20个字', trigger: 'blur' }
          ],
          /*hospitalType:[
            {  required: true, message: "请选择医院类型", trigger: "change" }
          ],
          areaIds:[
            {  required: true, message: "请选择所在区", trigger: "change" }
          ],
          hospitalAddress: [
            { required: true, message: "请输入详细地址", trigger: "blur" }
          ]*/
        }
      }
  },
  components: {
    Region
  },
  methods: {
    init(){
      if (this.$route.query.id) {
        this.dataForm.esId = this.$route.query.id
        this.getData()
        this.getTemperatureList()
      }else{
        this.getSyUserList(null)
        this.getTemperatureList()
      }
    },
    keydown(e) {
      if (e.keyCode == 32) {
        e.returnValue = false;
      }
    },
    sureClick() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
            .then(() => {
              if (this.dataForm.esId){
                this.editData();
              }else{
                this.addData();
              }
          });
        }
      })
    },
    addData() {
     let param = {
          boxName: this.dataForm.boxName,
          boxCode: this.dataForm.boxCode,
          syUserId: this.dataForm.syUserId,
          etId: this.dataForm.etId,
          boxType: this.dataForm.boxType
      };
      if ((param.syUserId == null || param.syUserId == "") && param.etId){
        this.$message.error('请选择收样员');
        return;
      }
      if ((param.etId == null || param.etId == "") && param.syUserId){
        this.$message.error('请选择温控设备');
        return;
      }
      EquipApi.addEquipSpecimen(param)
      .then(data => {
        this.$Utils.notifySuccess()
        this.returnList();
      })
    },
    editData() {
      let param = {
        esId: this.dataForm.esId,
        boxName: this.dataForm.boxName,
        boxCode: this.dataForm.boxCode,
        syUserId: this.dataForm.syUserId,
        etId: this.dataForm.etId,
        boxType: this.dataForm.boxType
      };
      if ((param.syUserId == null || param.syUserId == "") && param.etId){
        this.$message.error('请选择收样员');
      }
      if ((param.etId == null || param.etId == "") && param.syUserId){
        this.$message.error('请选择温控设备');
      }
      EquipApi.editEquipSpecimen(param)
        .then(data => {
          this.$Utils.notifySuccess()
          this.returnList();
        })
    },
    getData () {
      EquipApi.getEquipSpecimenInfo({
        esId: this.dataForm.esId
      }).then(res => {
        this.$Utils.bindingBean(this.dataForm, res.data)
        if (res.data.etId){
          this.dataForm.etId = parseInt(res.data.etId);
        }
        if (res.data.syUserId){
          this.dataForm.syUserId = parseInt(res.data.syUserId);
        }
        this.getSyUserList(this.dataForm.syUserId);
      })
    },
    getSyUserList (syUserId) {
      let param = {
        roleId: 4
      }
      if (syUserId){
        param.syUserId = syUserId
      }
      AuthApi.listAdminForSy(param).then(res => {
        this.syUserList = res.data
      })
    },
    getTemperatureList () {
      let _this = this
      EquipApi.getTempForSelect({
        esId: this.dataForm.esId
      }).then(res => {
        setTimeout(() => {
          _this.loadingIns = false
          _this.temperatureList = res.data.map(item => {
            return {
              value: item.etId,
              label: item.equipName
            }
          })
        }, 200)
        //this.temperatureList = res.data
      })
    },
    returnList(){
      this.$store.dispatch('delView', this.$route);
      this.$router.push({path: '/equip/specimenManagement'});
    }
  },
  mounted(){
    this.init()
  }
}
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope>
</style>
